// pages/NativeAPI/native-api.js
Page({
  // 网络
  handleRequest() {
    wx.request({
      // 请求路径
      url: 'https://gmall-prod.atguigu.cn/mall-api/index/findBanner',
      // 请求方法
      method: 'GET',
      // 请求参数
      data: {},
      // 请求头
      header: {},
      // 请求成功回调
      success: (res) => {console.log('请求成功回调',res)},
      // 请求失败回调
      fail: (err) => {console.log('请求失败回调',err)},
      // 请求完成回调：请求成功或失败都会执行
      complete: (res) => {console.log('请求完成回调',res)},
    })
  },
  // 界面
  handleShowLoading() {
    wx.showLoading({
      // 提示内容：不支持换行
      title: '加载中...',
      // 是否显示透明蒙层，防止触摸穿透
      mask: false,
      // 成功回调
      success: (res) => {console.log('成功回调',res)},
      // 失败回调
      fail: (err) => {console.log('失败回调',err)},
      // 完成回调：成功或失败都会执行
      complete: (res) => {console.log('完成回调',res)},
    })
  },
  handleHideLoading() {
    wx.hideLoading({
      // 成功回调
      success: (res) => {console.log('成功回调',res)},
      // 失败回调
      fail: (err) => {console.log('失败回调',err)},
      // 完成回调：成功或失败都会执行
      complete: (res) => {console.log('完成回调',res)},
    })
  },
  async handleShowModal() {
    wx.showModal({
      title: '提示 - complete',
      content: '确定删除？',
      success: (res) => {console.log('config-success',res)},
      fail: (err) => {console.log('config-fail',err)},
      complete: (res) => {
        console.log('config-complete',res)
        if (res.cancel) { console.log('config-complete - 取消') }
        if (res.confirm) { console.log('config-complete - 确定') }
      }
    })

    wx.showModal({
      title: '提示 - Promise',
      content: '确定删除？',
    }).then(res => {
      console.log('Promise-then',res)
      if (res.cancel) { console.log('Promise-then - 取消') }
      if (res.confirm) { console.log('Promise-then - 确定') }
    }).catch(err => {
      console.log('Promise-catch',err)
    })

    const awaitModal = await wx.showModal({
      title: '提示 - async/await',
      content: '确定删除？',
    })
    console.log('async/await',awaitModal)
    if (awaitModal.cancel) { console.log('async/await - 取消') }
    if (awaitModal.confirm) { console.log('async/await - 确定') }
  },
  // 缓存：查看调试器Storage
  handleSetStorage() {
    wx.setStorageSync('number-sync', 10)
    wx.setStorageSync('object-sync', { // 对象类型数据无需转为JSON，直接以对象类型进行存储
      username: 'syx',
      password: '********'
    })

    wx.setStorage({key: 'number', data: 20})
    wx.setStorage({
      key: 'object',
      data: {username:'syx', password:'********'}
    })
  },
  handleGetStorage() {
    const numberSync = wx.getStorageSync('number-sync')
    const objectSync = wx.getStorageSync('object-sync')
    console.log(numberSync) // 10
    console.log(objectSync) // {username: "syx", password: "********"}

    wx.getStorage({key:'number-sync'}).then(res=>{console.log(res)}) // {errMsg: "getStorage:ok", data: 10}
    wx.getStorage({key:'object-sync'}).then(res=>{console.log(res)}) // {errMsg: "getStorage:ok", data: {…}}
    wx.getStorage({key:'number'}).then(res=>{console.log(res)}) // {errMsg: "getStorage:ok", data: 20}
    wx.getStorage({key:'object'}).then(res=>{console.log(res)}) // {errMsg: "getStorage:ok", data: {…}}
  },
  handleRemoveStorage() {
    wx.removeStorageSync('number-sync')

    wx.removeStorage({key:'number'})
  },
  handleClearStorage() {
    wx.clearStorageSync()

    wx.clearStorage()
  },
  // 更新：添加编译模式，勾选下次编译时模拟更新
  handleGetUpdateManager() {
    const updateManager = wx.getUpdateManager();

    // 新版本下载完成后触发onUpdateReady
    updateManager.onUpdateReady(() => {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        complete: (res) => {
          if (res.confirm) {
            // 强制当前小程序使用新版本，并重启当前小程序
            updateManager.applyUpdate()
          }
        }
      })
    })
  },
})